package com.ldatum.crm.action.admin;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.ParentPackage;

import com.ldatum.crm.domain.TDepartment;
import com.ldatum.crm.domain.TEmployee;
import com.ldatum.crm.domain.TPosition;
import com.ldatum.crm.service.TDepartmentService;
import com.ldatum.crm.service.TEmployeeService;
import com.ldatum.crm.serviceImpl.TDepartmentServiceImpl;
import com.ldatum.crm.serviceImpl.TEmployeeServiceImpl;

@ParentPackage("admin")
public class EmployeeAction  extends BaseAction {

	private static final long serialVersionUID = 1L;
	private TEmployee employee = new TEmployee();
	
	static TEmployeeService employeeService = new TEmployeeServiceImpl();
	static TDepartmentService departmentService = new TDepartmentServiceImpl();
	
	List<TEmployee> employeeList = new ArrayList<TEmployee>();
	List<TDepartment> departmentList = new ArrayList<TDepartment>();
	
	private Integer id;
	private String fullname;
	private String loginname;
	private String password;
	private String headPicture;
	private String gender;
	private Date birthday;
	private Integer age;
	private String eduBackground;
	private int departmentId; 
	private int positionId;
	private String phone;
	private String telephone;
	private String comEmail;
	private String perEmail;
	private String address;
	private String birthPlace;
	private String persondesc;
	private String status;
	private Date employmentDate;
	private Date levelOffice;
	private String ids;
	private String oldpwd;
	private String newpwd;
	private String rpnewpwd;
	
	private String levelOne;
	private String levelTwo;
	
	public String list() throws Exception{
		employeeList = employeeService.list(TEmployee.class);
		levelOne = "employee";
		levelTwo = "employeeList";
		return "listEmployee";
	}
	
	public String add() throws Exception{
		departmentList = departmentService.list();
		employee.setLoginname("");
		employee.setPassword("");
		employee.setStatus("在职");
		TDepartment depart = new TDepartment();
		depart.setId(0);
		employee.setDepartment(depart);
		TPosition po = new TPosition();
		po.setId(0);
		employee.setPosition(po);
		levelOne = "employee";
		levelTwo = "employeeAdd";
		return "addEmployee";
	}
	
	public void save() throws Exception{
		PrintWriter out = null;
		JSONObject json = new JSONObject();
		try{
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("type/html");
			response.setCharacterEncoding("utf-8");
			out = response.getWriter();
			TDepartment d = new TDepartment();
			d.setId(departmentId);
			TPosition p = new TPosition();
			p.setId(positionId);
			employee.setDepartment(d);
			employee.setPosition(p);
			employeeService.save(employee);
			json.accumulate("result", true);
		}catch(Exception e){
			e.printStackTrace();
			json.accumulate("result", false);
		}finally{
			out.println(json.toString());
			out.flush();
			out.close();
		}
	}
	
	public String profile() throws Exception{
		levelOne="";
		levelTwo = "";
		employee = employeeService.findById(TEmployee.class, id);
		return "employee_profile";
	}

	public void deletes(){
		PrintWriter out = null;
		JSONObject json = new JSONObject();
		try{
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("type/html");
			response.setCharacterEncoding("utf-8");
			out = response.getWriter();
			employeeService.deletes(ids);
			json.accumulate("success", true);
		}catch(Exception e){
			e.printStackTrace();
			json.accumulate("success", false);
		}finally{
			out.println(json.toString());
			out.flush();
			out.close();
		}
	}
	
	public String edit() throws Exception{
		employee = employeeService.findById(TEmployee.class, id);
		departmentList = departmentService.list();
		levelOne = "employee";
		levelTwo = "employeeList";
		return "editEmployee";
	}
	
	public void update(){
		PrintWriter out = null;
		JSONObject json = new JSONObject();
		try{
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("type/html");
			response.setCharacterEncoding("utf-8");
			out = response.getWriter();
			TDepartment d = new TDepartment();
			d.setId(departmentId);
			TPosition p = new TPosition();
			p.setId(positionId);
			employee.setDepartment(d);
			employee.setPosition(p);
			employeeService.update(employee,employee.getId());
			json.accumulate("result", true);
		}catch(Exception e){
			e.printStackTrace();
			json.accumulate("result", false);
		}finally{
			out.println(json.toString());
			out.flush();
			out.close();
		}
	}
	
	public void changepwd(){
		PrintWriter out = null;
		JSONObject json = new JSONObject();
		try{
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("type/html");
			response.setCharacterEncoding("utf-8");
			out = response.getWriter();
			json.accumulate("result", employeeService.changepwd(id,oldpwd,newpwd,rpnewpwd));
		}catch(Exception e){
			e.printStackTrace();
			json.accumulate("result", false);
		}finally{
			out.println(json.toString());
			out.flush();
			out.close();
		}
	}
	
	public TEmployee getEmployee() {
		return employee;
	}

	public void setEmployee(TEmployee employee) {
		this.employee = employee;
	}

	public List<TEmployee> getEmployeeList() {
		return employeeList;
	}

	public void setEmployeeList(List<TEmployee> employeeList) {
		this.employeeList = employeeList;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public String getLevelOne() {
		return levelOne;
	}

	public void setLevelOne(String levelOne) {
		this.levelOne = levelOne;
	}

	public String getLevelTwo() {
		return levelTwo;
	}

	public void setLevelTwo(String levelTwo) {
		this.levelTwo = levelTwo;
	}

	public List<TDepartment> getDepartmentList() {
		return departmentList;
	}

	public void setDepartmentList(List<TDepartment> departmentList) {
		this.departmentList = departmentList;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getFullname() {
		return fullname;
	}

	public void setFullname(String fullname) {
		this.fullname = fullname;
	}

	public String getLoginname() {
		return loginname;
	}

	public void setLoginname(String loginname) {
		this.loginname = loginname;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getHeadPicture() {
		return headPicture;
	}

	public void setHeadPicture(String headPicture) {
		this.headPicture = headPicture;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	public String getEduBackground() {
		return eduBackground;
	}

	public void setEduBackground(String eduBackground) {
		this.eduBackground = eduBackground;
	}

	public int getDepartmentId() {
		return departmentId;
	}

	public void setDepartmentId(int departmentId) {
		this.departmentId = departmentId;
	}

	public int getPositionId() {
		return positionId;
	}

	public void setPositionId(int positionId) {
		this.positionId = positionId;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getTelephone() {
		return telephone;
	}

	public void setTelephone(String telephone) {
		this.telephone = telephone;
	}

	public String getComEmail() {
		return comEmail;
	}

	public void setComEmail(String comEmail) {
		this.comEmail = comEmail;
	}

	public String getPerEmail() {
		return perEmail;
	}

	public void setPerEmail(String perEmail) {
		this.perEmail = perEmail;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getBirthPlace() {
		return birthPlace;
	}

	public void setBirthPlace(String birthPlace) {
		this.birthPlace = birthPlace;
	}

	public String getPersondesc() {
		return persondesc;
	}

	public void setPersondesc(String persondesc) {
		this.persondesc = persondesc;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public Date getEmploymentDate() {
		return employmentDate;
	}

	public void setEmploymentDate(Date employmentDate) {
		this.employmentDate = employmentDate;
	}

	public Date getLevelOffice() {
		return levelOffice;
	}

	public void setLevelOffice(Date levelOffice) {
		this.levelOffice = levelOffice;
	}
	public String getOldpwd() {
		return oldpwd;
	}

	public void setOldpwd(String oldpwd) {
		this.oldpwd = oldpwd;
	}

	public String getNewpwd() {
		return newpwd;
	}

	public void setNewpwd(String newpwd) {
		this.newpwd = newpwd;
	}

	public String getRpnewpwd() {
		return rpnewpwd;
	}

	public void setRpnewpwd(String rpnewpwd) {
		this.rpnewpwd = rpnewpwd;
	}
	
}
